package com.xyyl.patient.controller;

import java.util.Map;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.xyyl.patient.common.utils.SysConfigUtils;
import com.xyyl.patient.core.constant.AppEnum;
import com.xyyl.patient.core.util.ResponseBodyResult;
import com.xyyl.patient.core.util.RestClient;
import com.xyyl.patient.core.util.StringUtils;
import com.xyyl.patient.entity.continuationonline.ContinuationOnline;
import com.xyyl.patient.service.continuationonline.ContinuationOnlineService;
import com.xyyl.patient.service.doctor.DoctorService;

@RestController
@RequestMapping("/continuation")
public class ContinuationOnlineController {
	private Logger logger = LogManager.getLogger(ContinuationOnlineController.class);
	
	@Autowired
	private  ContinuationOnlineService continuationOnlineService;
	
	@Autowired
	private DoctorService doctorService;
	
	/**
	 * @Title: submitOrder
	 * @Description: 提交订单 
	 * @param @param continuationOnline
	 * @param @return    参数
	 * @return JSONObject    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/submitOrder", method = RequestMethod.POST)
	public JSONObject submitOrder(  ContinuationOnline continuationOnline) {

		try {
			boolean flag=continuationOnlineService.checkUserOrder(continuationOnline);
			if(flag) {
				Integer orderId= continuationOnlineService.insertContinuationOnline(continuationOnline);
				
				//发送SMS通知
				sendSystemSMS(continuationOnline.getDoctorid());
				
				return ResponseBodyResult.success("下单成功", orderId);
			}else {
				return ResponseBodyResult.fail("已存在其他订单。下单失败");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return ResponseBodyResult.error("下单失败", e);
		}
	}
	
	/**
     * @Title: sendSystemSMS
     * @Description: 发送短信通知
     * @param  doctorid    参数
     * @return void    返回类型
     * @throws
     */
	public void sendSystemSMS(Integer doctorid) {

		logger.info("--- send sms---");
		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				try {
					String mobile = doctorService.queryDoctorMoblieById(doctorid);
					if (StringUtils.isEmpty(mobile)) {
						logger.error("发送失败,手机号码不能为空!");
					}
					String sms_api_url = SysConfigUtils.getConfigValue(AppEnum.sysConfig.OMS_SYSTEM_SMS_API_URL.name());
					Map<String, Object> paramMap = Maps.newHashMap();
					paramMap.put("mobile", mobile);
					paramMap.put("smsTemplateKey", AppEnum.smsTemplaeKeys.SMS_TEMPLATE_SERVICE_ORDER.name());
					RestClient.postForObject(sms_api_url, JSON.toJSONString(paramMap), String.class);

				} catch (Exception e) {
					logger.error("发送失败:" + e);
				}

			}
		});
	}

}
